package com.nage.components.touch;

import android.util.Log;

import com.nage.components.Component;
import com.nage.engine.GameObject;
import com.nage.input.Input;
import com.nage.input.InputResponder;
import com.nage.input.InputSystem;

public abstract class Touchable extends Component implements InputResponder {
	// A touchable is a way of creating an area on the screen that responds
	// to input events that touch it.
	
	float x, y, width, height; // a touchable has a position on the screen and a size

	public Touchable(GameObject p) {
		super(p);
		type = TOUCHABLE;
		x = parent.getX();
		y = parent.getY();
		width = 0;
		height = 0;
		InputSystem.registerResponder(this);
	}
	
	@Override
	public void onInputEvent(Input i) {
		// there has been some input since the update. Is it on the button?
		if(i.xDown > parent.x && i.xDown < parent.x+width) {
			// the user pressed their finger does between the left and right bounds of button
			if(i.yDown > parent.y && i.yDown < parent.y+height) {
				// user pressed between top and bottom bounds
				if(i.downEvent) {
					// only do a touch the first time it is pressed. don't repeat it if button held down.
					onTouch();
					Log.v("Touchable", "Touchable object sensed a touch on it");
				}
			}
		}
	}

	@Override
	public void update(long delta) {
	}

	public abstract void onTouch();

	public void setWidth(String w) {
		if(w!=null) {
			width = Float.parseFloat(w);
		}		
	}

	public void setHeight(String h) {
		if(h!=null) {
			height = Float.parseFloat(h);
		}		
	}
	
	@Override
	public byte getOwner() {
		return parent.m_Owner;
	}

}
